const {staticUrl} = require("../../services/config");

const app = getApp();
const {AskPage} = app._require("common/index");
const formMixin = app._require("mixins/form.mixin.js").default;
AskPage({
    /**
     * 页面的初始数据
     */
    name: "personal",
    mixins: [formMixin],
    data: {
        statusBar: app.globalData.StatusBar,
        customBar: app.globalData.CustomBar,
        navBarHeight: app.globalData.navBarHeight,//导航栏高度
        boxItems: ["意见反馈", "联系客服", "关于我们"],
        boxImgItems: ["mine_fackback.png", "mine_kf.png", "mine_about.png"],
        firstName: "",
        url: "",
        user: {},
    },
    onLoad: function (options) {
        console.log("appInfo", app);
        this.refreshCode();
    },
    checkShowAlert() {
        if (app.user.token.length > 0) {
            //判断一下
            let openId = app.user.userInfo.openId;
            let showAlert = app.user.userInfo.showAlert;
            var that = this;
            if (openId.length == 0 && showAlert == true) {
                // 绑定
                app.user.userInfo.showAlert = false;
                wx.showModal({
                    title: '微信绑定提醒',
                    content: '你还没有绑定微信，绑定微信后可接收微信消息提醒。',
                    cancelText: "绑定微信",//默认是“取消”
                    confirmText: "下次绑定",
                    success: function (res) {
                        if (res.confirm) {

                            // console.log('用户点击确定')
                        } else if (res.cancel) {
                            that.bindWxAction();
                        }
                    }
                })
            }
        }
    },
    async refreshCode(show) {

        let user_id = app.user.userInfo.id;
        if (!user_id) {
            return
        }

        const response = await this.$_server("user/refreshCode", {
            params: {
                userId: "?userId=" + app.user.userInfo.id
            },
        });
        let {data} = response;
        console.log(data);
        if (data.code != 200) {
            throw data;
        }
        this.setData({
            "code": data.data
        })

        if (show == true) {
            wx.showToast({
                title: '刷新成功',
                icon: "none"
            })
        }

        wx.setStorage({
            data: "",
            key: "person_code",
        })


    },
    onShow(options) {

        // console.log(options);

        let person_code = wx.getStorageSync("person_code");
        // console.log(person_code);

        if (typeof this.getTabBar === "function" && this.getTabBar()) {
            this.getTabBar().setData({
                active: "/" + this.route,
            });
        }

        this.loginActionSuccess();

        if (person_code == 1) {
            this.refreshCode();
        }

        this.checkShowAlert();

    },
    logout() {
        wx.showModal({
            title: "",
            content: "确认退出?",
            success: (res) => {
                if (res.confirm) {
                    app.logout();
                    this.setData({
                        user: {},
                        firstName: "",
                        url: ""
                    });
                    // console.log('用户点击确定')
                } else if (res.cancel) {
                    console.log("用户点击取消");
                }
            },
        });
    },
    login() {
        console.log(22222222);
        if (this.data.user.token) return;
        this.setData({
            show: true
        })
        // wx.navigateTo({
        //   url: '/components/login-new-view/index',
        // })
        // this.selectComponent("#pageBox").login();

    },
    loginActionSuccess() {
        // if (this.data.user.token) return;
        let firstInfoName = app.user.userInfo.name || "";

        let name = firstInfoName.substring(0, 1);
        // console.log(app.user.userInfo.headPortrait);
        console.log(app.user);
        var headPortrait = "";
        try {
            headPortrait = app.user.userInfo.headPortrait;
            headPortrait = headPortrait.replace((staticUrl + "/"), '');
            headPortrait = headPortrait.replace((staticUrl), '');
        } catch (e) {
        }

        // console.log(headPortrait);
        try {
            if (headPortrait.length > 0) {
                if (headPortrait.substring(0, 1) != "/") {
                    headPortrait = "/" + headPortrait
                }
                headPortrait = staticUrl + headPortrait;
            }

        } catch (e) {
        }

        this.setData({
            user: app.user,
            firstName: name,
            url: headPortrait,
        });
    },
    clickItemAction: function (e) {
        var index = e.currentTarget.dataset.index;
        if (index == 1) {
            wx.makePhoneCall({
                phoneNumber: "400-8618-890",
                success: function () {
                    console.log("拨打成功");
                },
                fail: function () {
                    console.log("拨打失败");
                },
            });

        } else if (index == 2) {
            wx.navigateTo({
                url: "./about/index",
            });
        }
    },

    clickLoginAction: function () {
        console.log("点击了登录");
    },
    clickUserInfoAction: function () {
        // console.log("进入详情");
        console.log(app.user.userInfo);

        if (app.user.token.length == 0) {
            return;
        }

        wx.navigateTo({
            url: "./detail/index",
        });
    },
    //图片加载出错，替换为默认图片
    avatarError(error) {
        // console.log("1111111");
        this.setData({
            // Url:'/assets/error.png'
            // url: "/assets/error.png",
        });
    },
    click_refreash() {
        console.log("刷新");
        this.refreshCode(true);
    },
    click_copy() {
        wx.setClipboardData({
            data: this.data.code,
            showToast: false,
            success: function (res) {
                wx.showToast({
                    title: '邀请码已复制，请发送给好友进行注册',
                    duration: 2000,
                    icon: 'none'
                });
            }
        })
        // console.log("复制");
    },
    loginSuccess(e) {
        this.checkShowAlert();
        let headPortrait = "";
        try {
            headPortrait = app.user.userInfo.headPortrait;
        } catch (e) {
        }

        this.setData({
            show: false,
            user: app.user,
            url: headPortrait
        });

    },
});
